package com.cnblsp2.android.study.dbandio.files;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.cnblsp2.android.study.dbandio.R;

public class SDCardFileActivity extends Activity {

	private EditText writerEtxt = null;
	private EditText showerETxt = null;
	private Button writerBtn = null;
	private Button showerBtn = null;

	private static final String FILE_NAME = "/cnblsp2.bin";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_file);

		writerEtxt = (EditText) this.findViewById(R.id.activity_file_writer_etxt);
		showerETxt = (EditText) this.findViewById(R.id.activity_file_show_etxt);
		writerBtn = (Button) this.findViewById(R.id.activity_file_writer_btn);
		showerBtn = (Button) this.findViewById(R.id.activity_file_show_btn);
		writerBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String content = writerEtxt.getText().toString();
				if (content == null || "".equals(content)) {
					Toast.makeText(SDCardFileActivity.this, "请输入文本信息!", Toast.LENGTH_SHORT).show();
					;
					return;
				}
				write(content);

			}
		});

		showerBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String context = read();
				if (context == null || "".equals(context)) {
					Toast.makeText(SDCardFileActivity.this, "请先输入并保存数据到文件!", Toast.LENGTH_SHORT);
					return;
				}
				showerETxt.setText(context);
			}
		});

	}

	private String read() {
		try {
			// 如果手机插入了SD卡，而且应用程序具有访问SD的权限

			if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
				// 获取SD卡对应的存储目录
				File sdCardDir = Environment.getExternalStorageDirectory();
				// 获取指定文件对应的输入流
				FileInputStream fis = new FileInputStream(sdCardDir.getCanonicalPath() + FILE_NAME);
				BufferedReader br = new BufferedReader(new InputStreamReader(fis));
				StringBuilder sb = new StringBuilder("");
				String line = null;
				while ((line = br.readLine()) != null) {
					sb.append(line);
				}
				return sb.toString();
			} else {
				Toast.makeText(SDCardFileActivity.this, "本应用没有SD卡权限!", Toast.LENGTH_SHORT).show();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public void write(String content) {
		try {

			if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
				// 获取SD卡的目录
				File sdCardDir = Environment.getExternalStorageDirectory();
				File targetFile = new File(sdCardDir.getCanonicalPath() + FILE_NAME);

				// 以指定文件创建RandomAccessFile对象
				RandomAccessFile raf = new RandomAccessFile(targetFile, "rw");

				// 将文件记录指针移动到最后
				raf.seek(targetFile.length());

				// 输入文件内容
				raf.write(content.getBytes());

				raf.close();

			} else {
				Toast.makeText(SDCardFileActivity.this, "本应用没有SD卡权限!", Toast.LENGTH_SHORT).show();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
